package com.fr.privilege.vote;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.privilege.Privilege;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authority.Authority;
import com.fr.privilege.manager.PrivilegeManager;

public class DefaultVote
  implements Vote
{
  private static DefaultVote defaultVote;

  public static DefaultVote getDefaultVote()
  {
    if (defaultVote == null)
      defaultVote = new DefaultVote();
    return defaultVote;
  }

  public boolean vote(Authentication paramAuthentication, Privilege paramPrivilege)
  {
    if (paramPrivilege == null)
      return true;
    Authority[] arrayOfAuthority1 = paramPrivilege.neededAuthority();
    if (ArrayUtils.isEmpty(arrayOfAuthority1))
      return true;
    if (!(FRContext.getPrivilegeManager().usePrivilege()))
      return true;
    if (paramAuthentication == null)
      return false;
    if (!(paramAuthentication.isAuthenticated()))
      return false;
    Authority[] arrayOfAuthority2 = paramAuthentication.getAuthorities();
    for (int i = 0; i < arrayOfAuthority2.length; ++i)
    {
      Authority localAuthority1 = arrayOfAuthority2[i];
      for (int j = 0; j < arrayOfAuthority1.length; ++j)
      {
        Authority localAuthority2 = arrayOfAuthority1[j];
        if (localAuthority2.equals(localAuthority1))
          return true;
      }
    }
    return false;
  }
}